home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <sys/time.h>
-
- #include "fft.h"
- #include "constant.h"
-
- /* */
- /* Precision dependant declarations */
- /* */
- #ifdef ZOMPLEX
-
- #define TOLERANCE DTOLERANCE
- typedef double this_half;
- typedef zomplex this_type;
-
- #define THIS_SUM dsum_
- #define THIS_GEN zgen_
- #define THIS_FT zft3d_
-
- #define THIS_FFTI zfft3di
- #define THIS_FFT zfft3d
- #define THIS_SCAL zscal3d
-
- #endif
- #ifdef COMPLEX
-
- typedef float this_half;
- typedef complex this_type;
-
- #define TOLERANCE STOLERANCE
-
- #define THIS_SUM ssum_
- #define THIS_GEN cgen_
- #define THIS_FT cft3d_
-
- #define THIS_FFTI cfft3di
- #define THIS_FFT cfft3d
- #define THIS_SCAL cscal3d
-
- #endif
-
- /* */
-
- this_half THIS_SUM();
-
- void inimat_();
- void GetArguments();
- void get_values();
-
- int size_x, ldx, size_y, ldy, size_z, n_trials;
- this_type *pa, *pb, *pRef, *pSave;
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int i, j, inc, n_total, is_wrong, nerr;
- double x, y, dx, dy, emax, sum, err, maxerr;
- this_half t;
- this_type *ptr, *pt;
-
- /* ******************************************************* */
- GetArguments( argc, argv);
- /* ******************************************************* */
-
- srandom( (123*getpid()) | 0x01);
-
- for( ; n_trials > 0; n_trials --) {
- get_values();
-
- printf("\n");
- printf( "%d <%3d,%3d,%3d>", n_trials, size_x, size_y, size_z);
- fflush(stdout);
-
- n_total = ((size_x+1)*(size_y+1)*(size_z+1));
- ldx = size_x + 1;
- ldy = size_y + 1;
- pa = (this_type *)malloc
- ( (3 * n_total + size_x + size_y + size_z + 3 * FACTOR_SPACE) * sizeof(this_type));
- if( !(pa) ) {
- fprintf( stderr, "Could not allocate ... Exiting\n");
- exit (-1);
- }
- pb = pa + n_total;
- pRef = pb + n_total;
- pSave = pRef + n_total;
-
- /* *******************************************************
- Initialize arrays
- ******************************************************* */
- THIS_GEN(pRef, &n_total);
- bcopy( pRef, pa, n_total*sizeof(this_type));
- bcopy( pRef, pb, n_total*sizeof(this_type));
-
- /* *******************************************************
- direct Fourier Transform
- ******************************************************* */
- j = -1;
- THIS_FT( &j, &size_x, &size_y, &size_z, pb, &ldx, &ldy);
- pSave = THIS_FFTI( size_x, size_y, size_z, pSave);
- is_wrong = THIS_FFT( -1, size_x, size_y, size_z, pa, ldx, ldy, pSave);
-
- emax = TOLERANCE*(size_x * size_y * size_z);
- for(i = 0, nerr=0 ; i < n_total ; i ++) {
- x = pa[i].re - pb[i].re;
- y = pa[i].im - pb[i].im;
- if( (t= x*x + y*y) > (emax)) {
- nerr++;
- }
- }
- if( !(nerr)){
- fprintf( stderr, ".");
- }
- else {
- fprintf( stderr, "@%d@", nerr);
- }
- inc = 2;
- x = y = 0;
- for( i= 0, pt = pRef; i < size_z ; i++, pt += ldx * ldy)
- for( j= 0, ptr = pt ; j < size_y ; j++, ptr += ldx) {
- x += THIS_SUM( &size_x, ptr, &inc);
- y += THIS_SUM( &size_x, ((char *)ptr) + sizeof(this_half), &inc);
- }
- dx = x - pa[0].re;
- dy = y - pa[0].im;
- if( (t= dx *dx + dy*dy) > TOLERANCE){
- fprintf( stderr,
- "\nError direct FFT at index #0 : (%f,%f)<->(%f,%f)error(%f,%f)",
- pa[0].re, pa[0].im,x,y, dx, dy);
- }
- dx = x - pb[0].re;
- dy = y - pb[0].im;
- if( (t= dx *dx + dy*dy) > TOLERANCE){
- fprintf( stderr,
- "\nError direct FT at index #0 : (%f,%f)<->(%f,%f)error(%f,%f)",
- pb[0].re, pb[0].im,x,y, dx, dy);
- }
- /* *******************************************************
- inverse Fourier Transform
- ******************************************************* */
- is_wrong = THIS_FFT( 1, size_x, size_y, size_z, pa, ldx, ldy, pSave);
-
- inc = 2;
- x = y = 0;
- for( i= 0, pt = pb; i < size_z ; i++, pt += ldx * ldy)
- for( j= 0, ptr = pt ; j < size_y ; j++, ptr += ldx) {
- x += THIS_SUM( &size_x, ptr, &inc);
- y += THIS_SUM( &size_x, ((char *)ptr) + sizeof(this_half), &inc);
- }
- dx = x - pa[0].re;
- dy = y - pa[0].im;
- nerr = 0;
- emax = size_x * size_y * size_z * TOLERANCE;
- if( (t= dx *dx + dy*dy) > TOLERANCE){
- fprintf( stderr, "#");
- nerr ++;
- }
- if( !(nerr)){
- fprintf( stderr, ".");
- }
-
- t = 1. / (double)(size_x * size_y * size_z);
- THIS_SCAL( size_x, size_y, size_z, t, pa, ldx, ldy);
-
- emax = TOLERANCE;
- emax = emax * emax;
-
- sum = 0.;
- err= 0.;
- nerr= 0;
- maxerr= 0.;
- for(i = 0 ; i < n_total ; i ++) {
- sum += (pRef[i].re * pRef[i].re) + (pRef[i].im * pRef[i].im);
- x = pa[i].re - pRef[i].re;
- y = pa[i].im - pRef[i].im;
- if( (t= x*x + y*y) > (emax))
- nerr++;
- err += t;
- if( t > maxerr) maxerr = t;
- }
- err = sqrt(err / (double)(size_x*size_y));
- sum = sqrt(sum / (double)(size_x*size_y));
- maxerr = sqrt(maxerr);
- printf(": avg:(%8.3g /%8.3g)= %8.3g <> maxerr: (%8.3g /%8.3g)= %8.3g",
- err, sum, err/sum, maxerr, sum, maxerr/sum);
- if(nerr){
- printf("\n%d errors detected \n", nerr);
- exit(-2);
- }
- free((char *)pa);
- }
- printf("\n");
- return(0);
- }
-
- int is_random;
-
- void GetArguments( argc, argv)
- int argc;
- char *argv[];
- {
- int i, j, k;
- int nerror = 0;
-
- #define ON 1
-
- n_trials = DEF_TRIALS;
- is_random = 1;
-
- /* ******************************************************* */
- for ( i = 1 ; (i < argc) && (nerror != ON) ; i ++ ) {
- if( argv[i][0] == '-') {
- switch ( argv[i][1]) {
- case 'i' :
- case 'I' :
- is_random = 0;
- break;
- default : nerror = ON;
- }
- }
- else {
- if( i+1 > argc)
- nerror = ON;
- else {
- n_trials = atoi( argv[i]);
- }
- }
- }
- if( nerror == ON) {
- fprintf( stderr,
- "Usage : %s [-p(arallel)] [n_trials]\n", argv[0]);
- exit(-1);
- }
- }
-
- void get_values()
- {
- if( is_random){
- size_x = random() % MAX_3D + 1;
- size_y = random() % MAX_3D + 1;
- size_z = random() % MAX_3D + 1;
- if( !(n_trials % 5)) {
- size_y = size_x;
- size_z = size_x;
- }
- } else{
- printf( "Enter SIZE_X : ");
- scanf("%d", &size_x);
- printf( "Enter SIZE_Y : ");
- scanf("%d", &size_y);
- printf( "Enter SIZE_Z : ");
- scanf("%d", &size_z);
- }
- }
-